package com.dstz.bpm.act.img;

import com.dstz.base.core.util.ThreadMapUtil;
import java.awt.Color;
import java.awt.Paint;
import java.awt.Stroke;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.RoundRectangle2D;
import java.awt.geom.RoundRectangle2D.Double;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.activiti.bpmn.model.AssociationDirection;
import org.activiti.bpmn.model.FlowNode;
import org.activiti.bpmn.model.GraphicInfo;
import org.activiti.image.impl.DefaultProcessDiagramCanvas;

public class BpmProcessDiagramCanvas extends DefaultProcessDiagramCanvas {
   public BpmProcessDiagramCanvas(int width, int height, int minX, int minY, String imageType, String activityFontName, String labelFontName, String annotationFontName, ClassLoader customClassLoader) {
      super(width, height, minX, minY, imageType, activityFontName, labelFontName, annotationFontName, customClassLoader);
      LABEL_COLOR = Color.BLACK;
      LABEL_FONT = null;
   }

   public void drawHighLight(int x, int y, int width, int height, Paint paint) {
      Paint originalPaint = this.g.getPaint();
      Stroke originalStroke = this.g.getStroke();
      this.g.setPaint(paint);
      this.g.setStroke(THICK_TASK_BORDER_STROKE);
      RoundRectangle2D rect = new Double((double)x, (double)y, (double)width, (double)height, 20.0D, 20.0D);
      this.g.draw(rect);
      this.g.setPaint(originalPaint);
      this.g.setStroke(originalStroke);
   }

   public void drawSequenceflow(int[] xPoints, int[] yPoints, boolean drawConditionalIndicator, boolean isDefault, boolean highLighted, Paint paint, double scaleFactor) {
      String connectionType = "sequenceFlow";
      AssociationDirection associationDirection = AssociationDirection.ONE;
      Paint originalPaint = this.g.getPaint();
      Stroke originalStroke = this.g.getStroke();
      this.g.setPaint(CONNECTION_COLOR);
      if (connectionType.equals("association")) {
         this.g.setStroke(ASSOCIATION_STROKE);
      } else if (highLighted) {
         this.g.setPaint(paint);
         this.g.setStroke(HIGHLIGHT_FLOW_STROKE);
      }

      for(int i = 1; i < xPoints.length; ++i) {
         Integer sourceX = xPoints[i - 1];
         Integer sourceY = yPoints[i - 1];
         Integer targetX = xPoints[i];
         Integer targetY = yPoints[i];
         java.awt.geom.Line2D.Double line = new java.awt.geom.Line2D.Double((double)sourceX, (double)sourceY, (double)targetX, (double)targetY);
         this.g.draw(line);
      }

      java.awt.geom.Line2D.Double line;
      if (isDefault) {
         line = new java.awt.geom.Line2D.Double((double)xPoints[0], (double)yPoints[0], (double)xPoints[1], (double)yPoints[1]);
         this.drawDefaultSequenceFlowIndicator(line, scaleFactor);
      }

      if (drawConditionalIndicator) {
         line = new java.awt.geom.Line2D.Double((double)xPoints[0], (double)yPoints[0], (double)xPoints[1], (double)yPoints[1]);
         this.drawConditionalSequenceFlowIndicator(line, scaleFactor);
      }

      if (associationDirection.equals(AssociationDirection.ONE) || associationDirection.equals(AssociationDirection.BOTH)) {
         line = new java.awt.geom.Line2D.Double((double)xPoints[xPoints.length - 2], (double)yPoints[xPoints.length - 2], (double)xPoints[xPoints.length - 1], (double)yPoints[xPoints.length - 1]);
         this.drawArrowHead(line, scaleFactor);
      }

      if (associationDirection.equals(AssociationDirection.BOTH)) {
         line = new java.awt.geom.Line2D.Double((double)xPoints[1], (double)yPoints[1], (double)xPoints[0], (double)yPoints[0]);
         this.drawArrowHead(line, scaleFactor);
      }

      this.g.setPaint(originalPaint);
      this.g.setStroke(originalStroke);
   }

   @Override
   public void drawExclusiveGateway(GraphicInfo graphicInfo, double scaleFactor) {
      Paint paint = this.g.getPaint();
      Map<String, Paint> gateMap = (Map)ThreadMapUtil.get("DefaultInstHistImgService_gateMap");
      FlowNode flowNode = (FlowNode)ThreadMapUtil.get("BpmProcessDiagramGenerator_flowNode");
      this.g.setPaint((Paint)gateMap.getOrDefault(flowNode.getId(), paint));
      super.drawExclusiveGateway(graphicInfo, scaleFactor);
      this.g.setPaint(paint);
   }

   @Override
   protected void drawMultilineText(String text, int x, int y, int boxWidth, int boxHeight, boolean centered) {
      AttributedString attributedString = new AttributedString(text);
      attributedString.addAttribute(TextAttribute.FONT, this.g.getFont());
      attributedString.addAttribute(TextAttribute.FOREGROUND, Color.black);
      AttributedCharacterIterator characterIterator = attributedString.getIterator();
      int currentHeight = 0;
      List<TextLayout> layouts = new ArrayList();
      String lastLine = null;
      LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator, this.g.getFontRenderContext());

      int currentY;
      int currentX;
      for(TextLayout layout = null; measurer.getPosition() < characterIterator.getEndIndex() && currentHeight <= boxHeight; currentHeight += currentX) {
         currentY = measurer.getPosition();
         layout = measurer.nextLayout((float)boxWidth);
         lastLine = text.substring(currentY, measurer.getPosition());
         currentX = Float.valueOf(layout.getDescent() + layout.getAscent() + layout.getLeading()).intValue();
         if (currentHeight + currentX > boxHeight) {
            if (!layouts.isEmpty()) {
               layouts.remove(layouts.size() - 1);
            }

            layouts.add(new TextLayout(lastLine, this.g.getFont(), this.g.getFontRenderContext()));
            break;
         }

         layouts.add(layout);
      }

      currentY = y + (centered ? (boxHeight - currentHeight) / 2 : 0);

      TextLayout textLayout;
      for(Iterator var16 = layouts.iterator(); var16.hasNext(); currentY = (int)((float)currentY + textLayout.getDescent() + textLayout.getLeading())) {
         textLayout = (TextLayout)var16.next();
         currentY = (int)((float)currentY + textLayout.getAscent());
         currentX = x + (centered ? (boxWidth - java.lang.Double.valueOf(textLayout.getBounds().getWidth()).intValue()) / 2 : 0);
         textLayout.draw(this.g, (float)currentX, (float)currentY);
      }

   }
}
